package com.service.webservice.interceptor;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.headers.Header;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class AuthInterceptor extends AbstractPhaseInterceptor<SoapMessage> {

	public AuthInterceptor() {
		super(Phase.PRE_INVOKE);
	}


	@Override
	public void handleMessage(SoapMessage msg) throws Fault {	
		List<Header> headers = msg.getHeaders();
		if (headers != null && headers.size() > 0) {
			Element element = (Element) headers.get(0).getObject();
			checkParam(element);
		}else{
			throw new Fault(new Exception("headers is null"));
		}
	}
	
	private void checkParam(Element element){
		NodeList childNodes = (NodeList) element.getChildNodes();
		String equipmentNO = "";
		String sendTime = "";

		//equipmentNO
		if(childNodes.item(0) == null){
			throw new Fault(new Exception("equipmentNO is null"));			
		}else {
			//check equipmentNO isvaild
			equipmentNO = childNodes.item(0).getTextContent();
		}
		
		//发送时间
		if(childNodes.item(1) == null){
			throw new Fault(new Exception("sendTime is null"));			
		}else {
			//no check
			sendTime = childNodes.item(1).getTextContent();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			sendTime = sdf.format(new Date(Long.valueOf(sendTime)));
		}
		
		System.err.println("equipmentNO:" + equipmentNO + " sendTime:" + sendTime);
		
	}

}
